<!-- 耗材使用情况（耗材出入库） -->
<template>
  <el-dialog
    title="耗材使用情况（耗材出入库）"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    width="750px"
    :before-close="cancel">
    <el-table :height="calTableHeight() - 150" :data="tableData">
      <el-table-column
        show-overflow-tooltip
        :label="$t('耗材名称')"
        align="center"
        prop="cesName"
        width="150" />
      <el-table-column show-overflow-tooltip :label="$t('业务类型')" align="center" prop="type" width="150">
        <template slot-scope="{ row }">
          <div :style="{ color: $randomColor(row.type) }">
            {{ handleType(row.type) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip :label="$t('操作数量')" align="center" prop="num" />
      <el-table-column show-overflow-tooltip :label="$t('操作前数量')" align="center" prop="beforeNum" />
      <el-table-column show-overflow-tooltip :label="$t('操作后数量')" align="center" prop="afterNum" />
      <el-table-column
        show-overflow-tooltip
        :label="$t('创建时间')"
        align="center"
        prop="createTime"
        width="150" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">
        <!-- 关闭 -->
        {{ $t('关闭') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getConsumableRecordsByCesId } from '@/api/purchasingManage/batch'
import { useDict } from '@/utils/useDict'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },

    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      total: 0,
      sku_consumable_type_list: [],
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  created() {
    useDict('sku_consumable_type').then(({ sku_consumable_type }) => {
      this.sku_consumable_type_list = sku_consumable_type
    })
  },
  methods: {
    handleType(type) {
      const index = this.sku_consumable_type_list.findIndex((i) => i.dictValue == type)
      return this.sku_consumable_type_list[index]?.dictLabel
    },
    getList() {
      this.tableData = []
      const data = {
        ...this.queryParams,
        cesId: this.formData.id,
      }
      getConsumableRecordsByCesId(data).then((res) => {
        this.tableData = res.rows
        this.total = res.total
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
